<template>
  <div class="app-container">
    <div class="queryform">
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="90px"
        id="llll"
      >
        <el-form-item label="风险类型:" prop="logtype">
          <el-select v-model="form.logtype" placeholder="全部">
            <el-option key="全部" label="全部" value=""> </el-option>
            <el-option
              v-for="item in logTypes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名称:" prop="intranetIp">
          <el-input
            v-model.trim="form.intranetIp"
            placeholder="请输入"
          ></el-input>
        </el-form-item>
        <el-form-item label="操作时间起:" prop="startTime">
          <el-date-picker
            v-model="form.startTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :clearable="false"
            type="datetime"
            placeholder="选择起始日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="操作时间止:" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            value-format="yyyy-MM-dd HH:mm:ss"
            :clearable="false"
            type="datetime"
            placeholder="选择截止日期时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="refresh" :disabled="loading"
            >查询</el-button
          >
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作栏 -->
    <!-- <div class="buttons">
      <el-button type="primary" icon="el-icon-plus" @click="add"
        >新增</el-button
      >
    </div> -->
    <div class="mod-result-table">
      <HTable
        ref="table"
        :tableData="TableData"
        :columnConfigs="columnConfigs"
        @selection-change="handleSelectionChange"
        :selectable="false"
        orderable
        v-loading="loading"
        :pages="pagination"
        @page-change="handlePageChange"
        @size-change="handleSizeChange"
      >
        <template slot-scope="{ row }">
          <template v-if="row.name == 'action'">
            <el-button
              type="text"
              size="small"
              icon="el-icon-edit"
              @click="edit(row.row)"
              >操作轨迹</el-button
            >
          </template>
          <template v-else-if="row.name == 'color'">
            <el-tag
              :style="{
                color: '#fff',
                backgroundColor: row.row.color,
                borderColor: row.row.color,
              }"
              effect="plain"
            >
              {{ row.row.color }}
            </el-tag>
          </template>
          <template v-else-if="row.name == 'size'">
            {{ row.row.size && row.row.size + "px" }}
          </template>
          <template v-else-if="row.name == 'rotation'">
            {{ row.row.rotation && row.row.rotation + "°" }}
          </template>
          <template v-else-if="row.name == 'transparency'">
            {{ row.row.transparency && formatTrancy(row.row.transparency) }}
          </template>
        </template>
      </HTable>
    </div>
    <div class="vpnform">
      <rule
        ref="AddDialog"
        :dialogVisible1.sync="dialogVisible1"
        :editFlag.sync="editFlag"
        @submit="submitaddForm"
      ></rule>
    </div>
  </div>
</template>

<script>
import table from "@/mixins/commontable";
import rule from "./rule.vue";
import { columnConfigs } from "./columnConfigs.js";
import { addList, editList, getList, delCurRow } from "@/api/yybl/watermark";
import { validateIpPort } from "@/utils/validate";
export default {
  components: {
    rule,
  },
  mixins: [table], //表格的混入，主要包括表格上通用的方法和数据，比如查询，重置，分页这些功能
  props: {},
  data() {
    let checkTransparency = (rex, value, callback) => {
      // if (!value) {
      //   return callback(new Error('年龄不能为空'));
      // }
      // const rule = new RegExp("^0.\d$");
      const rule = /^0.\d+$/;
      if (rule.test(value) || value == 0 || value == 1) {
        callback();
      } else {
        callback(new Error("透明度范围为0-1"));
      }
    };
    let checkNumber = (rex, value, callback) => {
      // if (!value) {
      //   return callback(new Error('年龄不能为空'));
      // }
      // const rule = new RegExp("^0.\d$");
      const rule = /^\d*$/;
      if (rule.test(value)) {
        callback();
      } else {
        callback(new Error("请输入数字"));
      }
    };
    return {
      colors: {
        // hex: "#194d33",
        // hex8: "#194D33A8",
        // hsl: { h: 150, s: 0.5, l: 0.2, a: 1 },
        // hsv: { h: 150, s: 0.66, v: 0.3, a: 1 },
        // rgba: { r: 25, g: 77, b: 51, a: 1 },
        // a: 1,
      },
      rules: [
        {
          label: "完全匹配",
          value: "0",
        },
        {
          label: "频次匹配",
          value: "1",
        },
      ],
      form: {},
      logTypes: [
        {
          label: "异常时间登录",
          value: "0",
        },
        {
          label: "异常IP登录",
          value: "1",
        },
      ],
      zhouqis: [
        {
          label: "月",
          value: "1",
        },
        {
          label: "周",
          value: "2",
        },
        {
          label: "日",
          value: "3",
        },
      ],
      columnConfigs,
      dialogVisible1: false,
      editFlag: false,
      fontOptions: [
        {
          value: "微软雅黑",
          label: "微软雅黑",
        },
        {
          value: "宋体",
          label: "宋体",
        },
        {
          value: "仿宋",
          label: "仿宋",
        },
        {
          value: "黑体",
          label: "黑体",
        },
        {
          value: "楷体",
          label: "楷体",
        },
        {
          value: "等线",
          label: "等线",
        },
      ],
      vpnform: {
        // rotation: new Number(),
        // size: new Number(),
      },
      rulesVpnform: {
        host: [
          { required: true, message: "请输入资源地址", trigger: "blur" },
          { validator: validateIpPort, trigger: "blur" },
          // { type: "number", message: "大小必须为数字值" },
        ],
        content: [
          { required: true, message: "请输入水印内容", trigger: "blur" },
          // { type: "number", message: "大小必须为数字值" },
        ],
        font: [
          { required: true, message: "请选择字体", trigger: "change" },
          // { type: "number", message: "大小必须为数字值" },
        ],
        size: [
          { validator: checkNumber, trigger: "blur" },
          { required: true, message: "请输入水印大小" },
          // { type: "number", message: "大小必须为数字值" },
        ],
        rotation: [
          { validator: checkNumber, trigger: "blur" },
          { required: true, message: "请输入旋转角度" },
          // { type: "number", message: "角度必须为数字值" },
        ],
        width: [
          { validator: checkNumber, trigger: "blur" },
          { required: true, message: "请输入宽度" },
          // { type: "number", message: "大小必须为数字值" },
        ],
        height: [
          { validator: checkNumber, trigger: "blur" },
          { required: true, message: "请输入高度" },
          // { type: "number", message: "大小必须为数字值" },
        ],
        transparency: [
          // { validator: checkTransparency, trigger: "blur" },
          { required: true, message: "请输入透明度", trigger: "blur" },
        ],
      },
      computers: [],
      vpntypes: [],
      curRow: {},
    };
  },
  watch: {},
  computed: {
    TableData() {
      const deshuiyin = (con) => {
        let a = con.replace(/\\([\<\>\'\"\\])/g, (d, b) => {
          return `${b}`;
        });
        return a;
      };
      let list = this.tableData.map((i) => {
        let val = deshuiyin(i.content);
        return {
          ...i,
          content: val,
        };
      });
      return list;
    },
  },
  methods: {
    formatTrancy(val) {
      let a = Number(val);
      return Math.round(a * 100) + "%";
    },
    formatTooltip(val) {
      return val + "%";
    },
    formatTooltipRotation(val) {
      return val + "°";
    },
    formatTooltipSize(val) {
      return val;
    },
    handleChange(val) {
      console.log(val);
    },
    add() {
      this.editFlag = false;
      this.$nextTick(() => {
        this.dialogVisible1 = true;
        this.$refs?.addform?.clearValidate();
      });
      this.$refs.AddDialog.add({
        host: "",
        content: "",
        rotation: 15,
        size: 16,
        font: "微软雅黑",
        color: "#f0f0f0",
        width: "250",
        height: "150",
        transparency: 50,
      });
      // this.vpnform = {
      //   host: "",
      //   content: "",
      //   rotation: 15,
      //   size: 16,
      //   font: "微软雅黑",
      //   color: "#f0f0f0",
      //   width: "250",
      //   height: "150",
      //   transparency: 50,
      // };
      // this.$refs?.addform?.clearValidate();
    },
    edit(row) {
      this.editFlag = true;
      this.dialogVisible1 = true;
      this.curRow = row;
      this.curRow.transparency = Number(this.curRow.transparency);
      this.curRow.rotation = Number(this.curRow.rotation);
      this.curRow.size = Number(this.curRow.size);
      this.$refs.AddDialog.edit(row);
      // const {
      //   host,
      //   content,
      //   size,
      //   rotation,
      //   font,
      //   color,
      //   width,
      //   height,
      //   transparency,
      // } = row;
      // this.vpnform = {
      //   host,
      //   content,
      //   size,
      //   rotation,
      //   font,
      //   color,
      //   width,
      //   height,
      //   transparency: Math.round(transparency * 100),
      // };
      this.$refs?.addform?.clearValidate();
    },
    async handleDelete(row) {
      this.$confirm("此操作将删除数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ids;
          if (row) ids = row.watermarkId;
          const res = await delCurRow({
            ids: ids,
          });
          if (res.rescode == "200") {
            this.$message.success("删除成功");
            this.refresh();
          } else this.$message.error(res.msg);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    submitaddForm(val) {
      console.log(val);
      // this.$refs["addform"].validate((val) => {
      //   if (val) {
      //     console.log("通过校验");
      //     const {
      //       host,
      //       content,
      //       size,
      //       rotation,
      //       font,
      //       color,
      //       width,
      //       height,
      //       transparency,
      //     } = this.vpnform;
      //     let a = content.replace(/[\<\>\'\"\\]/g, (a, b, c) => {
      //       return `\\${a}`;
      //     });
      //     let transparencyval = transparency / 100;
      //     let params = {
      //       host,
      //       content: a,
      //       size,
      //       rotation,
      //       font,
      //       color,
      //       width,
      //       height,
      //       transparency: transparencyval,
      //     };
      //     // params.userId = null;
      //     // params.sharedkey = encode(form.sharedkey);
      //     if (this.editFlag) {
      //       params.watermarkId = this.curRow.watermarkId;
      //       editList(params)
      //         .then((res) => {
      //           if (res.rescode == 200) {
      //             this.dialogVisible1 = false;
      //             this.$message.success("编辑成功");
      //             this.refresh();
      //           } else {
      //             this.$message.error(res.msg);
      //           }
      //         })
      //         .catch((err) => {
      //           console.log(err);
      //         });
      //     } else {
      //       // 调用新增接口
      //       addList(params)
      //         .then((res) => {
      //           if (res.rescode == 200) {
      //             this.dialogVisible1 = false;
      //             this.$message.success("新增成功");
      //             this.refresh();
      //           } else {
      //             this.$message.error(res.msg);
      //           }
      //         })
      //         .catch((err) => {
      //           console.log(err);
      //         });
      //     }
      //   } else {
      //     console.log("校验失败");
      //   }
      // });
    },
    handleClose() {
      this.dialogVisible1 = false;
    },
    handleSelectionChange() {},
    refresh() {
      // this.getlists(getList, {});
      this.tableData = [
        { 
          username:'huchengbo',
          watermarkId: 2,
          host: "异常时间登录",
          content: "2024-02-21 20:46:00",
          font: "微软雅黑",
          size: "16",
          color: "#f0f0f0",
          rotation: "15",
          refresh: null,
          width: "250",
          height: "150",
          transparency: "0.5",
          status: 0,
          remark: null,
          delFlag: 0,
          createBy: "admin",
          createTime: "2024-02-01T06:48:00.000+00:00",
          createTimeStr: "2024-02-01 14:48:00",
          updateBy: null,
          updateTime: "2024-02-01T06:48:00.000+00:00",
          updateTimeStr: "2024-02-01 14:48:00",
        },
        {
          username:'huchengbo',
          watermarkId: 1,
          host: "异常IP登录",
          content: "2024-02-21 20:38:20",
          font: "楷体",
          size: "19",
          color: "#5832EF",
          rotation: "20",
          refresh: null,
          width: "400",
          height: "200",
          transparency: "0.5",
          status: 0,
          remark: null,
          delFlag: 0,
          createBy: "admin",
          createTime: "2023-10-25T01:41:29.000+00:00",
          createTimeStr: "2023-10-25 09:41:29",
          updateBy: "admin",
          updateTime: "2024-02-21T10:56:39.000+00:00",
          updateTimeStr: "2024-02-21 18:56:39",
        },
      ];
      this.pagination.total = 2;
    },
  },
  created() {
    this.refresh();
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.queryform {
  // border: 1px solid #e8e8e8;
  padding: 10px;
  margin-bottom: 10px;
  ::v-deep .el-form {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      max-width: 33.33%;
      flex-grow: 1;
      flex-shrink: 1;
      flex-basis: 33.33%;

      .el-date-editor {
        width: 100%;
      }
    }
    // .el-form-item:nth-child(7) {
    //   max-width: 100%;
    //   flex-basis: 100%;
    //   display: flex;
    //   justify-content: end;
    // }
  }
}
::v-deep .el-form-item__content {
  .el-date-editor.el-input {
    width: 100%;
  }
}
::v-deep .el-button {
  .el-icon-edit + span {
    margin-left: 0px;
  }
  .el-icon-delete + span {
    margin-left: 0px;
  }
}
::v-deep .el-dialog {
  min-width: 640px;
  .el-dialog__header {
    border-bottom: 1px solid #e8e8e8;
  }
  .el-dialog__footer {
    border-top: 1px solid #e8e8e8;
  }
}
.addDialog {
  .el-dialog {
    min-width: 640px;
  }

  .el-form {
    .el-form-item.slider {
      width: 100%;
    }
  }
}
</style>
